package labs.utilities;

import static labs.utilities.PrintUtilities.printError;
import static labs.utilities.PrintUtilities.printException;
import static labs.utilities.PrintUtilities.printInfo;

import java.io.Closeable;

public abstract class LabsFileIO {
	private Closeable closable;
	private final String fileName;
	private boolean isOpened;

	public LabsFileIO(String fileName) {
		this(fileName, true);
	}

	public LabsFileIO(String fileName, boolean openOnCreation) {
		this.fileName = fileName;
		if (openOnCreation) {
			open();
		}
	}

	public abstract void open();

	public void close() {
		if (!isOpened) {
			printInfo("Stream Object is not opened to be closed.");
			return;
		}
		try {
			closable.close();
			isOpened = false;
		} catch (Exception ex) {
			printError("Couldn't close reader.");
			printException(ex);
		}
	}

	/**
	 * @param closable
	 *            the stream object to set
	 */
	public void setClosable(Closeable closable) {
		this.closable = closable;
	}

	/**
	 * @return the Stream Object
	 */
	public Closeable getStreamObject() {
		return closable;
	}

	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @param isOpened
	 *            the isOpened to set
	 */
	public void setOpened(boolean isOpened) {
		this.isOpened = isOpened;
	}

	/**
	 * @return the isOpened
	 */
	public boolean isOpened() {
		return isOpened;
	}

}
